package gis.uzd_1;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultTreeSelectionModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import it.cnr.imaa.essi.lablib.gui.checkboxtree.CheckboxTree;
import it.cnr.imaa.essi.lablib.gui.checkboxtree.TreeCheckingEvent;
import it.cnr.imaa.essi.lablib.gui.checkboxtree.TreeCheckingListener;
import it.cnr.imaa.essi.lablib.gui.checkboxtree.TreeCheckingModel;

/**
 * Layer bar.
 * 
 * @version 0.1
 * @date 2010-04-19
 * @author Edmundas Matusevicius
 */
public class GISLayer extends CheckboxTree {
    public GISLayer() {
        setMinimumSize(new Dimension(140, 0));
        
        dataRoot.add(new DefaultMutableTreeNode("Data layers"));
        dataNode.add(new ArrayList<DefaultMutableTreeNode>());
        root.add(dataRoot.get(0));
        
        // only single selection model
        
        TreeSelectionModel selectionModel = new DefaultTreeSelectionModel();
        selectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        // set parameters
        
        setSelectionModel(selectionModel);
        setModel(new DefaultTreeModel(root));
        setRootVisible(false);
        setRowHeight(ROW_HEIGHT);
        getCheckingModel().setCheckingMode(TreeCheckingModel.CheckingMode.PROPAGATE_PRESERVING_CHECK);
        
        // checking model
        
        checkingModel = getCheckingModel();
        checkingModel.addCheckingPath(new TreePath(root.getPath()));
        checkingModel.addCheckingPath(new TreePath(dataRoot.get(0).getPath()));
        
        setUpActions();
    }
    
    private void setUpActions() {
        addTreeCheckingListener(new TreeCheckingListener() {
            public void valueChanged(TreeCheckingEvent e) {
                // if root checked
                if (e.getPath().getLastPathComponent().toString() == dataRoot.get(0).toString()) {
                    if (e.isCheckedPath()) {
                        setMapRootLayerVisible(true, e);
                    } else {
                        setMapRootLayerVisible(false, e);
                    }
                // if node checked
                } else {
                    if (e.isCheckedPath()) {
                        setMapNodeLayerVisible(true, e);
                    } else {
                        setMapNodeLayerVisible(false, e);
                    }
                }
            }
            
            private void setMapNodeLayerVisible(boolean visible, TreeCheckingEvent e) {
                for (int i = 0; i < GISMap.mapContext.getLayerCount(); i++) {
                    if (GISMap.mapContext.getLayer(i).getTitle() == e.getPath().getLastPathComponent().toString()) {
                        GISMap.mapContext.getLayer(i).setVisible(visible);
                        break;
                    }
                }
            }
            
            private void setMapRootLayerVisible(boolean visible, TreeCheckingEvent e) {
                TreeNode root = dataRoot.get(0);
                for (int i = 0; i < root.getChildCount(); i++) {
                    for (int j = 0; j < GISMap.mapContext.getLayerCount(); j++) {
                        if (GISMap.mapContext.getLayer(j).getTitle() == root.getChildAt(i).toString()) {
                            GISMap.mapContext.getLayer(j).setVisible(visible);
                            break;
                        }
                    }
                }
            }
        });
        
        addTreeSelectionListener(new TreeSelectionListener() {
            @SuppressWarnings("static-access")
            public void valueChanged(TreeSelectionEvent arg0) {
                DataPanel.mapPanel.resetLayers();
                DataPanel.mapPanel.selectedFeatures = null;
            }
        });
    }
    
    // data lists
    
    static TreeCheckingModel checkingModel;
    // grand root of the tree
    static DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
    // 0 item - "Data layers"
    static ArrayList<DefaultMutableTreeNode> dataRoot = new ArrayList<DefaultMutableTreeNode>();
    // 0 item - "Data layers" layers
    static ArrayList<ArrayList<DefaultMutableTreeNode>> dataNode = new ArrayList<ArrayList<DefaultMutableTreeNode>>();
    
    private final static int ROW_HEIGHT = 18;
}
